<template>
  <div class="user-center">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="user-info">
          <div class="avatar">
            <el-avatar :size="120" :src="userInfo.avatar" />
          </div>
          <div class="info">
            <div class="name">{{ userInfo.name }}</div>
            <div class="role">{{ userInfo.role }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>个人信息</span>
            </div>
          </template>
          <el-form 
            ref="infoForm"
            :model="userInfo" 
            label-width="80px"
            label-position="left"
          >
            <el-form-item label="姓名">
              <el-input v-model="userInfo.name" disabled />
            </el-form-item>
            <el-form-item label="工号">
              <el-input v-model="userInfo.empId" disabled />
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="userInfo.gender" disabled>
                <el-radio label="male">男</el-radio>
                <el-radio label="female">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="部门">
              <el-input v-model="userInfo.department" disabled />  
            </el-form-item>
            <el-form-item label="角色">
              <el-input v-model="userInfo.role" disabled />
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="userInfo.phone" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="userInfo.email" />  
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSave">保存</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-20">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>修改密码</span>
            </div>
          </template>
          <el-form
            ref="pwdForm"
            :model="pwdForm"
            :rules="pwdRules"
            label-width="120px"
            label-position="left"
          >
            <el-form-item label="原密码" prop="oldPwd">
              <el-input v-model="pwdForm.oldPwd" type="password" show-password />
            </el-form-item>
            <el-form-item label="新密码" prop="newPwd">
              <el-input v-model="pwdForm.newPwd" type="password" show-password />
            </el-form-item>
            <el-form-item label="确认新密码" prop="confirmPwd">
              <el-input v-model="pwdForm.confirmPwd" type="password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleChangePwd">确认修改</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>操作日志</span>
            </div>  
          </template>
          <el-timeline>
            <el-timeline-item
              v-for="log in operationLogs"
              :key="log.id"
              :timestamp="log.timestamp"
            >
              {{ log.content }}  
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const userInfo = ref({
  name: '张三',
  empId: '1001',
  gender: 'male',
  department: '技术部',
  role: '管理员',
  phone: '13800138000',
  email: 'zhangsan@company.com',
  avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
})

const infoForm = ref(null)

const handleSave = () => {
  infoForm.value.validate(valid => {
    if (valid) {
      // 实际调用修改个人信息的API
      ElMessage.success('个人信息修改成功')
    }
  })
}

const pwdForm = reactive({
  oldPwd: '',
  newPwd: '',
  confirmPwd: ''
})

const pwdRules = {
  oldPwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }
  ],
  confirmPwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== pwdForm.newPwd) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const pwdFormRef = ref(null)

const handleChangePwd = () => {
  pwdFormRef.value.validate(valid => {
    if (valid) {
      // 实际调用修改密码的API
      ElMessage.success('密码修改成功,请重新登录')
    }
  })
}

const operationLogs = ref([
  { id: 1, timestamp: '2023-06-01 10:20:30', content: '登录系统' },
  { id: 2, timestamp: '2023-06-01 10:25:20', content: '创建采购订单:PO20230601001' },
  { id: 3, timestamp: '2023-06-01 11:10:10', content: '创建销售订单:SO20230601001' }
])
</script>

<style scoped>
.user-center {
  padding: 20px;
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
}

.user-info .info {
  margin-top: 20px;
  text-align: center;
}

.user-info .name {
  font-size: 24px;
  font-weight: bold;
}

.user-info .role {
  margin-top: 10px;
  color: var(--el-text-color-secondary);
}

.mt-20 {
  margin-top: 20px;
}
</style> 